package com.aaa.stock.controller.api;

import com.aaa.entity.Medicines;
import com.aaa.entity.Producer;
import com.aaa.stock.service.MedicinesService;
import com.aaa.stock.vo.MedicinesVo;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("stock/api/med")
public class ApiMedicinalController {

    @Autowired
    private MedicinesService medicinesService;

    @PostMapping("semed/{currentPage}/{pageSize}")
    public Result<IPage<Medicines>> semd(
            @PathVariable Integer currentPage,
            @PathVariable Integer pageSize,
            @RequestBody MedicinesVo medicinesVo
            ){
        return  medicinesService.findByConditio(currentPage,pageSize,medicinesVo);
    }


    @PostMapping("addStu")
    public Result addstu(@RequestBody Medicines medicines) {
        if (Objects.nonNull(medicines.getMedicinesId())){
            medicines.setUpdateTime(LocalDateTime.now());
        }else {
            medicines.setCreateTime(LocalDateTime.now());
        }
        medicinesService.saveOrUpdate(medicines);
        return new Result<>(200,"成功");
    }


    @DeleteMapping("/{medicinesId}")
    public Result delStu(@PathVariable Integer medicinesId){
        medicinesService.removeById(medicinesId);
        return new Result<>(200,"删除成功");
    }


    @PostMapping("/del1")
    public Result delStu1(@RequestBody List<Medicines> medicines){
        medicines.forEach((Medicines ppp)->{
            boolean b = medicinesService.removeById(ppp.getMedicinesId());
        });
        return new Result(200,"成功");
    }
}
